package tv.youxing.android.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

import tv.youxing.android.R;


public class ImageLoaderConfig {

	
	public static DisplayImageOptions initDisplayOptions(boolean isShowDefault) {
		DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder();
		// ????????????
				// EXACTLY: ??????????????��??????��
				// EXACTLY_STRETCHED: ?????????????��
				// IN_SAMPLE_INT: ???????��???????????
				// IN_SAMPLE_POWER_OF_2: ????????2????????????????�k?????��??????��
				// NONE: ?????????
		displayImageOptionsBuilder.imageScaleType(ImageScaleType.EXACTLY);
		if (isShowDefault) {
			
			displayImageOptionsBuilder.showStubImage(R.drawable.kuang);
			
			displayImageOptionsBuilder
					.showImageForEmptyUri(R.drawable.kuang);
			displayImageOptionsBuilder.showImageOnFail(R.drawable.kuang);
		}
		
		displayImageOptionsBuilder.cacheInMemory(true);
		displayImageOptionsBuilder.cacheOnDisc(true);
		displayImageOptionsBuilder.bitmapConfig(Bitmap.Config.RGB_565);

		return displayImageOptionsBuilder.build();
	}

	/**
	 * �����޸�ͼƬ��С�ļ��ز�����??
	 * 
	 * @return
	 */
	public static DisplayImageOptions initDisplayOptions(int targetWidth,
                                                         boolean isShowDefault) {
		DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder();
		
		displayImageOptionsBuilder.imageScaleType(ImageScaleType.EXACTLY);
		if (isShowDefault) {
			
			displayImageOptionsBuilder.showStubImage(R.drawable.kuang);

			displayImageOptionsBuilder.showImageForEmptyUri(R.drawable.kuang);

			displayImageOptionsBuilder.showImageOnFail(R.drawable.kuang);
		}
		displayImageOptionsBuilder.cacheInMemory(true);
		
		displayImageOptionsBuilder.cacheOnDisc(true);
		
		displayImageOptionsBuilder.bitmapConfig(Bitmap.Config.RGB_565);
		
		displayImageOptionsBuilder.displayer(new SimpleImageDisplayer(
				targetWidth));

		return displayImageOptionsBuilder.build();
	}

	/**
	 * �첽ͼƬ����ImageLoader�ĳ�ʼ����������Application�е��ô˷���
	 * 
	 * @param context
	 *            �����Ķ�??
	 * @param cacheDisc
	 *            ͼƬ���浽SDCard��Ŀ¼��ֻ��Ҫ����SDCard��Ŀ¼�µ���Ŀ¼���ɣ�Ĭ�ϻὨ����SDcard�ĸ�Ŀ¼??
	 */
	public static void initImageLoader(Context context, String cacheDisc) {
		
		File cacheDir = StorageUtils.getOwnCacheDirectory(context, cacheDisc);
		
		ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
				context);
		
		builder.threadPoolSize(3);
		
		builder.threadPriority(Thread.NORM_PRIORITY);
		
		builder.memoryCache(new WeakMemoryCache());
		
		builder.memoryCacheExtraOptions(480, 800);
	
		builder.denyCacheImageMultipleSizesInMemory();
		
		builder.discCache(new UnlimitedDiscCache(cacheDir));
		
		builder.discCacheFileNameGenerator(new HashCodeFileNameGenerator());
	
		builder.imageDownloader(new BaseImageDownloader(context, 10000, 60000));
	
		builder.defaultDisplayImageOptions(initDisplayOptions(true));

		
		ImageLoader.getInstance().init(builder.build());
	}
}
